package io.github.enbool.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.github.enbool.bean.ResultBean;
import io.github.enbool.bean.query.DeviceQuery;
import io.github.enbool.domain.Device;
import io.github.enbool.service.DeviceService;
import io.github.enbool.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/*
 * device api
 * @author enbool
 *
 */
@Api(description = "Device管理接口")
@RestController
@RequestMapping(value = "/api/devices")
public class DeviceController { 		

	@Autowired
	private DeviceService deviceService;
	
	
	@ApiOperation(value = "查询Device")
	@PostMapping(value = "/query")
	public ResultBean<Page<Device>> query(@RequestBody(required = false) DeviceQuery query, Pageable pageable) {
		
		if(query == null) {
			query = new DeviceQuery();
		}
		query.setCreateUser(UserUtil.getUser().getId());
		return new ResultBean<>(deviceService.query(query, pageable));
	}
	
	@ApiOperation(value = "根据id查找Device")
	@GetMapping(value = "/{id:\\d+}")
	public ResultBean<Device> findOne(@PathVariable Long id){
		return new ResultBean<>(deviceService.findOne(id));
	}
	
	@ApiOperation(value = "新增Device")
	@PostMapping
	public ResultBean<Device> create(@RequestBody Device device){
		return new ResultBean<>(deviceService.create(device));
	}
	
	@ApiOperation(value = "更新Device")
	@PutMapping
	public ResultBean<Device> update(@RequestBody Device device){
		return new ResultBean<>(deviceService.update(device));
	}
	
	@ApiOperation(value = "根据id删除Device")
	@DeleteMapping(value = "/{id:\\d+}")
	public ResultBean<Long> delete(@PathVariable Long id){
		return new ResultBean<>(deviceService.delete(id));
	}
}